﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using VH.SharePoint.Api.Blogger;
using VH.SharePoint.Api.Queries;

namespace VH.SharePoint.Api.SharePoint
{
    public class SPBlogRepository : BlogRepository
    {
        private BlogWeb Blog { get; set; }

        public override IEnumerable<Blog> FindAll()
        {
            return new Blog[]
                {
                    new Blog
                        {
                            EndPointUrl = Blog.ApiUrl,
                            //Adding ListId for WLW
                            Id = Blog.Id+"#"+Blog.PostLibraryId,
                            Name = Blog.Name,
                            IsAdmin = Blog.IsAdmin,
                            Url = Blog.Url
                        }
                };
        }

        public override IEnumerable<Blog> FindAll(QueryConstraints<Blog> constraints)
        {
            return FindAll();
        }

        public override Blog Get(string id)
        {
            var index = id.IndexOf("#", System.StringComparison.Ordinal);
            if (index != -1)
                id = id.Substring(0, index);
            var blog = FindAll().First();
            if(new Guid(blog.Id)!=new Guid(id))throw new ArgumentException("The blog id is not correct","id");
            return blog;
        }

        public override void Create(Blog entity)
        {
            throw new NotSupportedException();
        }

        public override void Update(Blog entity)
        {
            throw new NotSupportedException();
        }

        public override void Delete(Blog entity)
        {
            throw new NotSupportedException();
        }

        public override void ValidateUser(string username, string password)
        {
            
        }

        protected override void Initialize()
        {
            Blog = BlogWeb.GetBlogWeb(SPContext.Current.Web);
            Posts = new SPPostRepository(Blog);
            Medias = new SPMediaRepository(Blog);
        }
    }
}
